<template>
	<view class="main-box">
		<image src="../../static/yitop.png" mode="widthFix" style="width: 100%;"></image>
		<submit-form :param="props.param"></submit-form>
		<image @click="toTopMethod" class="bg" mode="widthFix" src="../../static/yibg.png">
		</image>

	</view>
	<goto-form-fix-btn :targetId="scrollToTargetId"></goto-form-fix-btn>

</template>

<script setup>
	import VueScrollTo from "vue-scrollto"
	import {
		onMounted,
		ref
	} from "vue";
	import httpUtil from "@/utils/httpUtil.js"
	import deviceUtil from "@/utils/deviceUtil.js"
	import userApi from "@/api/userApi.js"
	import aliApi from "../../api/aliApi";
	import orderApi from "@/api/orderApi.js"
	import commonApi from "@/api/commonApi.js"

	const scrollToTargetId = ref('form-box')
	const scrollData = ref(0);

	const props = defineProps(['a', 'param', 'code'])

	onMounted(() => {
		// 监听滚动条位置
		window.addEventListener("scroll", scrollTopMethod, true);
		setTimeout(() => {
			toTopMethod();
		}, 1000)
		if (props.a) {
			httpUtil.setA(props.a)
		}
		initData()
	})

	const initData = () => {
		// #ifdef H5
		commonApi.valid404()
		// #endif
		orderApi.setBuyType(2)
		orderApi.setOrderType(17)
		if (props.param) {
			userApi.setParam(props.param)
		}
		// #ifdef H5
		// if (userApi.autoGotoSx2b2cCom()) {
		// 	return;
		// }
		// 如果是微信浏览器
		console.log('initData1')
		if (deviceUtil.isWeChatBrowser()) {
			console.log('initData2')
			// 用户没有userId 就获取
			if (!userApi.getUserId() && !props.code) {
				console.log('initData3')
				userApi.gotoH5WxPlatformLoginUrl()
			}
			console.log('initData4')
			if (!userApi.getUserId() && props.code) {
				console.log('initData5')
				userApi.initUserId(props.code)
			}
			// 支付宝浏览器登录
		}
		// else if (deviceUtil.isAliBrowser()) {
		// 	aliApi.getAliUserIdMethod()
		// }

		// #endif
		// #ifdef MP
		initMiniData()
		// #endif

	}
	const initMiniData = () => {
		userApi.getMiniLoginCode(code => {
			userApi.initUserId(code)
		})
	}
	const scrollTopMethod = () => {
		scrollData.value = document.documentElement.scrollTop || document.body.scrollTop;
	}

	const toTopMethod = () => {
		console.log('toTopMethod', scrollToTargetId.value)
		let offset = document.documentElement.clientHeight / 2 - 100;
		VueScrollTo.scrollTo(document.getElementById(scrollToTargetId.value), 1000, {
			offset: -offset,
		});
	}
</script>

<style lang="scss">
	.main-box {
		position: relative;
	}

	.bg {
		width: 100%;
		display: block;
	}
</style>